// 爱吃香蕉的珂珂

import Foundation

func minEatingSpeed(_ piles: [Int], _ h: Int) -> Int {
    var min = 1
    var max = piles.max()!
    var mid = 0
    
    while min < max {
        mid = min + (max - min) / 2
//        print("min",min)
//        print("max",max)
//        print("mid",mid)
        if eatingTimeIsEnough(piles, h, mid){
            max = mid
        }
        else{
            min = mid + 1
        }
    }
    return max
}

func eatingTimeIsEnough(_ piles:[Int], _ h:Int, _ speed:Int) -> Bool{
    var time = 0
    for num in piles{
        time += Int(ceil(Double(num)/Double(speed)))
    }
    return time <= h
}
